#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <sdkhooks>

Handle hCvar_Trigger_timer,hCvar_Trigger_dis;
float Trigger_timer,Trigger_dis;

public Plugin myinfo =
{
	name = "坦克拳头，石头，技能伤害!",
	description = "坦克拳头，石头，技能伤害!",
	author = "灵现江湖",
	version = "1.0",
	url = "qq群64854516"
};


public void OnPluginStart()
{
	HookEvent("tank_spawn", Event_Tank_Spawn, EventHookMode_Pre);
	
	hCvar_Trigger_timer = CreateConVar("l4d2_hCvar_Trigger_timer", "15.0", "坦克每多少秒发动秒杀技能?", FCVAR_NOTIFY);
	HookConVarChange(hCvar_Trigger_timer, eConvarChanged);
	
	hCvar_Trigger_dis = CreateConVar("l4d2_hCvar_Trigger_dis", "300.0", "玩家与坦克多少距离之内，玩家被秒杀?", FCVAR_NOTIFY);
	HookConVarChange(hCvar_Trigger_dis, eConvarChanged);
	
	AutoExecConfig(true, "l4d2_tank_dama");
}

public OnConfigsExecuted()
{
	CvarsChanged();
}

public void eConvarChanged(Handle hCvar, const char[] sOldVal, const char[] sNewVal)
{
	CvarsChanged();
}

void CvarsChanged()
{
	Trigger_timer = GetConVarFloat(hCvar_Trigger_timer);
	Trigger_dis = GetConVarFloat(hCvar_Trigger_dis);
}

public OnClientPutInServer(client)
{
	SDKHook(client, SDKHook_OnTakeDamage, OnTakeDamage);
}

public void OnClientDisconnect(int client)
{
	SDKUnhook(client, SDKHook_OnTakeDamage, OnTakeDamage);
}


public void Event_Tank_Spawn(Handle:event, const String:name[], bool:dontBroadcast) 
{
	new userid = GetEventInt(event, "userid");
	new Client = GetClientOfUserId(userid);
	if (IsInfected(Client)) 
	{
		CreateTimer(Trigger_timer, nexthealing_inf, userid, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);	
	}
}

public Action nexthealing_inf(Handle timer, any client)
{
	client = GetClientOfUserId(client);
	if(IsTank(client) && IsPlayerAlive(client))
	{
		//PrintToChatAll("\x05 坦克非常愤怒，发动了秒杀！");
		new Float:pos[3];
		new Float:entpos[3];
		new Float:distance;
			

		GetEntPropVector(client, Prop_Send, "m_vecOrigin", pos);
		for (new i = 1; i <= MaxClients; i++)
		{
			if (!IsSurvivor(i) || !IsPlayerAlive(i))
				continue;
			GetEntPropVector(i, Prop_Send, "m_vecOrigin", entpos);
			distance = GetVectorDistance(pos, entpos);
			if (distance<=Trigger_dis)
			{
				ForcePlayerSuicide(i);
				PrintToChatAll("\x05[提示]\x03%N\x04杀死了\x03%N",client,i);
			}
		}
	}
	return Plugin_Continue;
}

public Action:OnTakeDamage(victim, &attacker, &inflictor, &Float:damage, &damageType, &weapon, Float:damageForce[3], Float:damagePosition[3]) 
{
	if(IsTank(attacker) && IsSurvivor(victim))
	{	
		decl String:WeaponUsed[64];
		if(IsValidEdict(weapon)) 
		{
			GetEdictClassname(weapon, WeaponUsed, sizeof(WeaponUsed));
		}
		else 
		{
			if(IsValidEdict(inflictor))
				GetEdictClassname(inflictor, WeaponUsed, sizeof(WeaponUsed));
		}
		if(StrContains(WeaponUsed,"tank_claw")!=-1 || StrContains(WeaponUsed,"tank_rock")!=-1)
		{
			ForcePlayerSuicide(victim);
			PrintToChatAll("\x05[提示]\x03%N\x04杀死了\x03%N",attacker,victim);
		}
	}
}

stock bool:IsSurvivor(client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 2;
}

stock bool:IsInfected(client)
{
	return client > 0 && client <= MaxClients && IsClientInGame(client) && GetClientTeam(client) == 3;
}

stock bool:IsTank(client)  
{
	if (!IsInfected(client))
		return false;

	if (GetEntProp(client, Prop_Send, "m_zombieClass") != 8)
		return false;

	return true;
}